package com.hao.editorview;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hao.editorbox.R;
import com.hao.editorview.adapter.EmojiViewPageAdapter;
import com.hao.editorview.fragment.EmojiFragment;
import com.hao.editorview.model.EmojiUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hao on 16/10/19.
 */
public class EditorViewActivity extends FragmentActivity implements View.OnClickListener {


    private EditText mContent;
    private ViewPager viewPager;
    private ImageView comment_emo;
    private TextView comment_submit;
    private RelativeLayout comment_layout;
    private RelativeLayout comment_rt;
    private ImageView comment_emoji;


    private String content;

    private boolean isComment = false;

    private List<Fragment> fragmentList;
    private EmojiViewPageAdapter emojiViewPageAdapter;
    private int count = 0;
    private static final int PAGE_COUNT = 27;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_info_comment);
        mContent = (EditText) findViewById(R.id.comment_content);
        viewPager = (ViewPager) findViewById(R.id.comment_viewpage);
        comment_emo = (ImageView) findViewById(R.id.comment_emo);
        comment_submit = (TextView) findViewById(R.id.comment_submit);
        comment_layout = (RelativeLayout) findViewById(R.id.comment_layout);
        comment_rt = (RelativeLayout) findViewById(R.id.comment_rt);
        comment_emoji = (ImageView) findViewById(R.id.comment_emoji);
        content = getIntent().getExtras().getString("content", "");
        mContent.setText(content);
        initClick();
        Editable etext = mContent.getText();
        if (etext != null)
            Selection.setSelection(etext, etext.length());
        initEmoji();
    }

    private void initClick() {
        comment_emo.setOnClickListener(this);
        comment_submit.setOnClickListener(this);
        comment_layout.setOnClickListener(this);
        comment_rt.setOnClickListener(this);
        comment_emoji.setOnClickListener(this);
        mContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPager.getVisibility() == View.VISIBLE) {
                    viewPager.setVisibility(View.GONE);
                    showKeyboard(mContent);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.comment_emo:
                break;
            case R.id.comment_submit:
                sendComment();
                break;
            case R.id.comment_layout:
                back();
                break;
            case R.id.comment_rt:

                break;
            case R.id.comment_emoji:
                if (viewPager.getVisibility() == View.GONE) {
                    hideKeyboard(mContent);
                    viewPager.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    /**
     * 设置返回值
     */
    private void back() {
        Intent intent = getIntent();
        intent.putExtra("isComment", isComment);
        intent.putExtra("content", mContent.getText().toString());
        setResult(RESULT_OK, intent);
        hideKeyboard(mContent);
        finish();
    }


    //评论
    public void sendComment() {
        String text = mContent.getText() + "";
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(this, "评论内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        isComment = true;
        back();
    }


    private void initEmoji() {
        List<String> list = EmojiUtils.getInstance().getEmojiStr();
        count = list.size() / PAGE_COUNT;
        if (list.size() % PAGE_COUNT > 0)
            count++;
        if (count <= 0)
            return;
        fragmentList = new ArrayList<>();
        for (int i = 1; i < count; i++) {
            EmojiFragment emojiFragment = new EmojiFragment();
            List<String> listItem = new ArrayList<>();
            for (int j = (i - 1) * PAGE_COUNT; j < i * PAGE_COUNT; j++) {
                if (j < list.size())
                    listItem.add(list.get(j));
            }
            emojiFragment.setData(listItem);
            setOnItemClickListener(emojiFragment);
            fragmentList.add(emojiFragment);
        }
        emojiViewPageAdapter = new EmojiViewPageAdapter(getSupportFragmentManager());
        emojiViewPageAdapter.setData(fragmentList);
        viewPager.setAdapter(emojiViewPageAdapter);
    }

    //设置点击时间
    private void setOnItemClickListener(EmojiFragment emojiFragment) {
        emojiFragment.setOnItemClickListener(new EmojiFragment.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id, String content) {
                String oldContent = mContent.getText() + "";
                mContent.setText(oldContent + content);
                Editable etext = mContent.getText();
                Selection.setSelection(etext, etext.length());
            }

            @Override
            public void onDeleteContent() {
                String content = mContent.getText() + "";
                if (TextUtils.isEmpty(content))
                    return;
                String newString = content.substring(0, content.length() - 1);
                mContent.setText(newString);
                Editable etext = mContent.getText();
                Selection.setSelection(etext, etext.length());
            }
        });
    }

    /**
     * 隐藏键盘
     */
    protected void hideKeyboard(EditText v) {
        v.setFocusable(false);
        v.setFocusableInTouchMode(false);
        v.clearFocus();
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
    }


    //弹起软键盘
    public void showKeyboard(EditText v) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        v.requestFocus();
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
    }

}
